package com.jeff.tianti.cms.dao;

import com.jeff.tianti.cms.dto.AwardRecordQueryDTO;
import com.jeff.tianti.cms.dto.CertificateQueryDTO;
import com.jeff.tianti.cms.dto.font.PrizeFontDTO;
import com.jeff.tianti.cms.entity.AwardRecord;
import com.jeff.tianti.cms.entity.Certificate;
import com.jeff.tianti.common.dao.CustomBaseSqlDaoImpl;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xujianfang
 * @desc ArticleDaoImpl类
 * @date 2017-03-16
 */

public class AwardRecordDaoImpl extends CustomBaseSqlDaoImpl implements AwardRecordCustom {

    public PageModel<AwardRecord> queryAwardRecordPage(AwardRecordQueryDTO awardRecordQueryDTO) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select a from AwardRecord a where 1=1 ");
        if (awardRecordQueryDTO != null) {
            if (StringUtils.isNotBlank(awardRecordQueryDTO.getName())) {
                hql.append(" and a.name like :name ");
                map.put("name", "%" + awardRecordQueryDTO.getName() + "%");
            }

            if (StringUtils.isNotBlank(awardRecordQueryDTO.getCompany())) {
                hql.append(" and a.company = :company ");
                map.put("company", awardRecordQueryDTO.getCompany());
            }

            if (StringUtils.isNotBlank(awardRecordQueryDTO.getColumnId())) {
                hql.append(" and a.columnId = :columnId ");
                map.put("columnId", awardRecordQueryDTO.getColumnId());
            }

            if (StringUtils.isNotBlank(awardRecordQueryDTO.getStartAwardTime())) {
                hql.append(" and a.awardTime > :awardTime ");
                map.put("awardTime", awardRecordQueryDTO.getStartAwardTime());
            }

            if (StringUtils.isNotBlank(awardRecordQueryDTO.getEndAwardTime())) {
                hql.append(" and a.awardTime > :endAwardTime ");
                map.put("endAwardTime", awardRecordQueryDTO.getEndAwardTime());
            }
        }
        return this.queryForPageWithParams(hql.toString(), map, awardRecordQueryDTO.getCurrentPage(), awardRecordQueryDTO.getPageSize());
    }

    public List<AwardRecord> queryAwardRecordPage(PrizeFontDTO prizeFontDTO) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("select a from AwardRecord a where 1=1 ");
        if (prizeFontDTO != null) {

            if (StringUtils.isNotBlank(prizeFontDTO.getColumnId())) {
                hql.append(" and a.columnId = :columnId ");
                map.put("columnId", prizeFontDTO.getColumnId());
            }
            String awardTimeStr = prizeFontDTO.getAwardTime();
            if (StringUtils.isNotBlank(awardTimeStr)) {

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");

                try {
                    Date awardTimeStartDate = sdf.parse(awardTimeStr);
                    hql.append(" and a.awardTime > :awardTimeStartDate ");
                    map.put("awardTimeStartDate", awardTimeStartDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }

                String awardTimeEnd = DateUtils.geDateByDelayYear(awardTimeStr, 1);

                try {
                    Date awardTimeEndDate = sdf.parse(awardTimeEnd);
                    hql.append(" and a.awardTime < :awardTimeEndDate ");
                    map.put("awardTimeEndDate", awardTimeEndDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            hql.append(" order by  a.awardTime desc");
        }

        return this.queryByMapParams(hql.toString(), map);
    }

    @Override
    public int updateSelectiveById(AwardRecord awardRecord) {
        Map<String, Object> map = new HashMap<String, Object>();
        StringBuilder hql = new StringBuilder();
        hql.append("update AwardRecord t set");
        if (awardRecord != null) {
            if (!StringUtils.isNotBlank(awardRecord.getId())) {
                throw new RuntimeException("id 不能为空");
            }

            if (StringUtils.isNotBlank(awardRecord.getName())) {
                hql.append(" t.name = :name ");
                map.put("name", awardRecord.getName());
            }

            if (StringUtils.isNotBlank(awardRecord.getCompany())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.company = :company ");
                } else {
                    hql.append(" t.company = :company ");
                }
                map.put("company", awardRecord.getCompany());
            }
            if (StringUtils.isNotBlank(awardRecord.getColumnId())) {
                if (hql.toString().contains("=")) {
                    hql.append(" ,t.columnId = :columnId ");
                } else {
                    hql.append(" t.columnId = :columnId ");
                }
                map.put("columnId", awardRecord.getColumnId());
            }

            if (StringUtils.isNotBlank(awardRecord.getLevel())) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.level = :level ");
                } else {
                    hql.append(" t.level = :level ");
                }
                map.put("level", awardRecord.getLevel());
            }

            if (awardRecord.getAwardTime() != null) {

                if (hql.toString().contains("=")) {
                    hql.append(" ,t.awardTime = :awardTime ");
                } else {
                    hql.append(" t.awardTime = :awardTime ");
                }
                map.put("awardTime", awardRecord.getAwardTime());
            }


            hql.append("where t.id=:id ");
            map.put("id", awardRecord.getId());
        }
        return this.update(hql.toString(), map);
    }
}
